#include "enemy_9.h"

extern int G_W, G_H;
extern QPoint shuttle_pos;

ENEMY_9::ENEMY_9(QPoint begin, QPoint end, int otklonenie, SCENE *scene, QTimeLine *global_tl, CASH *cash):QGraphicsObject()
{

    go = new SOUND_POWER(cash,"Res/Sound/Enemy/Enemy_9/go.ogg",this);
    go->Play_Now();

    this->begin=begin;

    this->setPos(begin);

    qsrand(QTime::currentTime().second());

    this->otklonenie=otklonenie;

    point=35;

    border = cash->cash_poly->value("Res/Ships/Enemy/Enemy_9/Enemy_9.poly");
    im = cash->cash_pixmap->value("Res/Ships/Enemy/Enemy_9/Enemy_9.png");

    this->setZValue(0.5);

    fp_left = QPoint(28,63);
    fp_right = QPoint(66,63);

    loop=0;

    anim_enemy = new QGraphicsItemAnimation();

    enemy_time_line = new QTimeLine(10000);
    enemy_time_line->setEasingCurve( QEasingCurve::Type(1));
    enemy_time_line->setFrameRange(0, 1);
    anim_enemy->setTimeLine(enemy_time_line);
    anim_enemy->setItem(this);

    anim_enemy->setPosAt(0,begin);
    anim_enemy->setPosAt(1,end);

    enemy_time_line->start();

    collisia_control = new COLLISIA_CONTROL(scene,((QGraphicsObject*)this),this,global_tl,cash,10);

    connect(collisia_control,SIGNAL(Collisia(QPointF,int)),this,SLOT(Damage_Detected(QPointF,int)));
    connect(enemy_time_line,SIGNAL(finished()),SLOT(Must_Move()));

}

QPainterPath ENEMY_9::shape() const
{

    return *border;

}

QRectF ENEMY_9::boundingRect() const
{

    return QRectF( QPointF(0,0),im->size() );

}

void ENEMY_9::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{

    painter->save();
    painter->drawPixmap( 0,0,*im );
    painter->restore();

}

void ENEMY_9::Move_To_Target() // Следовать к цели
{

    enemy_time_line->setEasingCurve( QEasingCurve::Type(17));

    if( (1+qrand()%(3-1+1))==3){                                                            // шанс выстрела 33.3%
        emit Fire( QPoint( this->pos().x()+fp_left.x(),this->pos().y()+fp_left.y() ) );
        emit Fire( QPoint( this->pos().x()+fp_right.x(),this->pos().y()+fp_right.y() ) );

        QTimer::singleShot(100, this,SLOT(Emit_Fire()));
        QTimer::singleShot(200, this,SLOT(Emit_Fire()));    // Если нужен больше залп добавить сингл шотов
        QTimer::singleShot(300, this,SLOT(Emit_Fire()));
    }

        enemy_time_line->setDuration(2000);
        anim_enemy->setPosAt(0,this->pos());
        anim_enemy->setPosAt(1,QPoint(shuttle_pos.x()+otklonenie,this->pos().y()) );
        enemy_time_line->start();

}

void ENEMY_9::Step_Back_Now()   // Команда отступить
{

    enemy_time_line->setEasingCurve( QEasingCurve::Type(1));

    enemy_time_line->setDuration(1000);
    anim_enemy->setPosAt(0,this->pos());
    anim_enemy->setPosAt(1,QPoint(begin.x()+qrand()%(begin.x()+100-begin.x()+1),begin.y()+qrand()%(begin.y()+100-begin.y()+1)));
    enemy_time_line->start();

}

void ENEMY_9::Emit_Fire()
{

    emit Fire( QPoint( this->pos().x()+fp_left.x(),this->pos().y()+fp_left.y() ) );
    emit Fire( QPoint( this->pos().x()+fp_right.x(),this->pos().y()+fp_right.y() ) );

}

void ENEMY_9::Damage_Detected(QPointF a, int d)
{

    a = QPointF();
    point-=d;

    if(point<=0)
        if(this->scene()){
            disconnect(enemy_time_line,SIGNAL(finished()),this,SLOT(deleteLater()));
            emit Kill_Me_Please((QGraphicsObject*)this);
        }

}

ENEMY_9::~ENEMY_9()
{
}

int ENEMY_9::type() const
{

    return point;

}

void ENEMY_9::Must_Move()
{

    if(loop==0)
        Move_To_Target();

    if(loop==1)
        Move_To_Target();

    if(loop==2)
        Move_To_Target();

    if(loop==3)
        Step_Back_Now();

    if(loop==4)
        this->deleteLater();


    ++loop;

}


